package com.buildings.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.bigkoo.pickerview.OptionsPickerView;
import com.buildings.R;
import com.buildings.api.Api;
import com.buildings.base.BaseActivity;
import com.buildings.bean.DivisionResultBean;
import com.buildings.http.BaseCallBack;
import com.buildings.http.MyHttpClient;
import com.buildings.util.CustomToast;
import com.buildings.util.DialogUtils;
import java.io.IOException;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

public class TableListActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack;
    private TextView tvTitle, tvRightText;
    //省
    private EditText etProvince;
    //市
    private EditText etCity;
    //区
    private EditText etArea;

    private RecyclerView rvTaskList;

    public static final String PROVINCE = "province";
    public static final String CITY = "city";
    public static final String COUNTY = "county";

    private List<DivisionResultBean.Division> provinceList;
    private List<DivisionResultBean.Division> cityList;
    private List<DivisionResultBean.Division> countyList;
    private DivisionResultBean.Division province;
    private DivisionResultBean.Division city;
    private DivisionResultBean.Division county;

    private Dialog myDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table_list);
        initView();
    }

    private void initView() {
        ivBack = findViewById(R.id.iv_back);
        tvTitle = findViewById(R.id.tv_title);
        tvRightText = findViewById(R.id.tv_right_text);
        etProvince = findViewById(R.id.et_province);
        rvTaskList = findViewById(R.id.rv_task_list);
        etCity = findViewById(R.id.et_city);
        etArea = findViewById(R.id.et_area);
        ivBack.setOnClickListener(this);
        tvRightText.setOnClickListener(this);
        etProvince.setOnClickListener(this);
        etCity.setOnClickListener(this);
        etArea.setOnClickListener(this);
        tvTitle.setText("探槽-点");
        tvRightText.setText("保存");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.et_province:
                if (myDialog == null) {
                    myDialog = DialogUtils.showRefreshDialog(activity);
                } else {
                    myDialog.show();
                }
                getDivisions("root", PROVINCE);
                break;
            case R.id.et_city:
                if (TextUtils.isEmpty(etProvince.getText().toString())) {
                    CustomToast.showToast(activity, "请先选择省");
                    return;
                }
                if (null != province
                        && "Municipality".equals(province.getDivisionType())){
                    return;
                }else {
                    if (myDialog == null) {
                        myDialog = DialogUtils.showRefreshDialog(activity);
                    } else {
                        myDialog.show();
                    }
                    getDivisions(province.getDivisionId(), CITY);
                }
                break;
            case R.id.et_area:
                if (TextUtils.isEmpty(etProvince.getText().toString())) {
                    CustomToast.showToast(activity, "请先选择省");
                    return;
                }
                if (TextUtils.isEmpty(etCity.getText().toString())) {
                    CustomToast.showToast(activity, "请先选择市");
                    return;
                }
                if (myDialog == null) {
                    myDialog = DialogUtils.showRefreshDialog(activity);
                } else {
                    myDialog.show();
                }
                getDivisions(city.getDivisionId(), COUNTY);
                break;
            case R.id.tv_right_text:
                //TODO 调用保存表单接口
                break;
        }
    }

    private void getDivisions(String divisionid, final String type) {
        String url = Api.GET_LOCATION + "divisions/" + divisionid + "/subdivisions";
        MyHttpClient
                .newBuilder()
                .get()
                .url(url)
                .build()
                .enqueue(new BaseCallBack<DivisionResultBean>() {
                    @Override
                    public void onSuccess(DivisionResultBean divisionResultBean) {
                        super.onSuccess(divisionResultBean);
                        if (myDialog != null)
                            myDialog.dismiss();
                        if (divisionResultBean != null && divisionResultBean.getCode() == 200) {
                            if (divisionResultBean.getData() != null) {
                                if (TextUtils.equals(PROVINCE, type)) {
                                    provinceList = divisionResultBean.getData();
                                    showPickerView(provinceList, PROVINCE);
                                } else if (TextUtils.equals(CITY, type)) {
                                    cityList = divisionResultBean.getData();
                                    showPickerView(cityList, CITY);
                                } else if (TextUtils.equals(COUNTY, type)) {
                                    countyList = divisionResultBean.getData();
                                    showPickerView(countyList, COUNTY);
                                }
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        CustomToast.showToast(activity, "网络异常");
                        if (myDialog != null)
                            myDialog.dismiss();
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        CustomToast.showToast(activity, "网络异常");
                        if (myDialog != null)
                            myDialog.dismiss();
                    }
                });
    }

    private void showPickerView(final List<DivisionResultBean.Division> list, final String type) {// 弹出选择器（省市区三级联动）
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                DivisionResultBean.Division division = list.get(options1);
                switch (type) {
                    case PROVINCE:
                        province = division;
                        if (null != province
                                && "Municipality".equals(province.getDivisionType())) {
                            city = division;
                            etCity.setText(division.getDivisionName());
                        } else {
                            city = null;
                            etCity.setText("");
                        }
                        county = null;
                        etArea.setText("");
                        break;
                    case CITY:
                        city = division;
                        county = null;
                        etArea.setText("");
                        etCity.setText(division.getDivisionName());
                        break;
                    case COUNTY:
                        county = division;
                        etArea.setText(division.getDivisionName());
                        break;
                }
            }
        })
                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK)
                .setContentTextSize(20)
                .build();
        pvOptions.setPicker(list);
        pvOptions.show();
    }
}
